package com.jy.adscm.operation.mscyb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.cg.bean.CgMaterial;
import com.jy.adscm.operation.cg.mapper.mysql.CgMaterialMapper;
import com.jy.adscm.operation.mscyb.bean.JqylMaterialdetail;
import com.jy.adscm.operation.mscyb.bean.JqylMaterialdetailReback;
import com.jy.adscm.operation.mscyb.bean.QueryJqylMaterialdetailReback;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqylMaterialdetailMapper;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqylMaterialdetailRebackMapper;
import com.jy.adscm.operation.mscyb.service.JqylMaterialService;
import com.jy.adscm.operation.mscyb.service.JqylMaterialdetailRebackService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-09-10
 */
@Service
public class JqylMaterialdetailRebackServiceImpl extends ServiceImpl<JqylMaterialdetailRebackMapper, JqylMaterialdetailReback> implements JqylMaterialdetailRebackService {
    @Autowired
    JyUserMapper jyUserMapper;
    @Autowired
    JqylMaterialdetailMapper jqylMaterialdetailMapper;
    @Autowired
    CgMaterialMapper cgMaterialMapper;
    @Autowired
    JqylMaterialService  jqylMaterialService;
    @Override
    public boolean updateJqylMaterialdetailReback(JqylMaterialdetailReback jqylMaterialdetailReback, Token token) throws AdscmSystemException {
            //查询之前的信息
        JqylMaterialdetailReback oldJqylMaterialdetailReback = super.selectById(jqylMaterialdetailReback.getId());
        if (!AdscmBaseUtils.getInstance().isNotEmpty(oldJqylMaterialdetailReback.getResult(),null)){
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqylMaterialdetailReback.getResult(),null)){
                if (jqylMaterialdetailReback.getResult()==1){
                    //返回原料库
                    JqylMaterialdetail jqylMaterialdetail = jqylMaterialdetailMapper.selectById(jqylMaterialdetailReback.getTid());
                    jqylMaterialService.updateJqylMaterialyValue( jqylMaterialdetail.getTid(),jqylMaterialdetail.getUsenums().floatValue());
                    jqylMaterialdetailReback.setState(1);
                }
            }
        }
        jqylMaterialdetailReback.setCreator(token.getUser().getId().toString());
        boolean b = super.updateById(jqylMaterialdetailReback);
        return b;
    }

    @Override
    public Page<JqylMaterialdetailReback> select(Page<JqylMaterialdetailReback> page, QueryJqylMaterialdetailReback queryJqylMaterialdetailReback) {
        Wrapper<JqylMaterialdetailReback> wrapper=new EntityWrapper<>();
        wrapper.eq("state",queryJqylMaterialdetailReback.getState());
        page=  super.selectPage(page,wrapper);
        List<JqylMaterialdetailReback> jqylMaterialdetailRebacks = page.getRecords();
        for (JqylMaterialdetailReback jqylMaterialdetailReback : jqylMaterialdetailRebacks) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(  jqylMaterialdetailReback.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(jqylMaterialdetailReback.getCreator());
                jqylMaterialdetailReback.setCreatorName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqylMaterialdetailReback.getTid(),null)){
                JqylMaterialdetail jqylMaterialdetail = jqylMaterialdetailMapper.selectById(jqylMaterialdetailReback.getTid());
                if (AdscmBaseUtils.getInstance().isNotEmpty(jqylMaterialdetail,null)){
                    if (AdscmBaseUtils.getInstance().isNotEmpty(jqylMaterialdetail.getUseperson(),null)){
                        JyUser jyUser = jyUserMapper.selectById(jqylMaterialdetail.getUseperson());
                        jqylMaterialdetail.setUsepersonName(jyUser.getName());
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(jqylMaterialdetail.getName(),null)){
                        CgMaterial cgMaterial = cgMaterialMapper.selectById(jqylMaterialdetail.getName());
                        jqylMaterialdetail.setNameName(cgMaterial.getName());
                    }
                    DecimalFormat df=new DecimalFormat("#0.00");
                    String format = df.format(jqylMaterialdetail.getUsenums());
                    jqylMaterialdetail.setUsenums(Double.valueOf(format));

                }
                jqylMaterialdetailReback.setJqylMaterialdetail(jqylMaterialdetail);
            }
        }
      page.setTotal(selectCount(wrapper));
        return page;
    }
}
